﻿using MediatR;
using MS.BasicData.Domain;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.IBaseRepository;

namespace MS.BasicData.Read.API.Applications.Permissionss
{
    public class ListPermiRequestHandler : IRequestHandler<ListPermiRequestModel, ApiRestult>
    {
        private readonly IPermisBaseBaseRepository _permissionsRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="permissionsRepository"></param>
        public ListPermiRequestHandler(IPermisBaseBaseRepository permissionsRepository)
        {
            _permissionsRepository = permissionsRepository;
        }

        public async Task<ApiRestult> Handle(ListPermiRequestModel request, CancellationToken cancellationToken)
        {
            ApiRestult restult = new ApiRestult
            {
                isSuc = true,
                Message = "获取权限信息列表成功",
                Code = Restult.Success
            };

            var permissions = await _permissionsRepository.GetAllAsync(a=>a.IsDeleted == false);
            if (!string.IsNullOrEmpty(request.PermissionsName))
            {
                permissions = permissions.Where(a => a.PermissionsName.Contains(request.PermissionsName)).ToList();
            }

            restult.Data = permissions;             
            return restult;
        }
    }


    public class ListPermiRequestModel : IRequest<ApiRestult>
    {
        /// <summary>
        /// 权限名称
        /// </summary>
        public string PermissionsName { get; set; } = string.Empty;

    }
}
